﻿Imports System.Collections.Generic
Imports System.Data

Partial Class WebForm_CommonControl_UploadPicControl
    Inherits System.Web.UI.UserControl

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
        Me.Controls.Add(Me.LoadControl("~/WebForm/CommonControl/GoogleMapExif.ascx"))
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(Button1)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not hiFileUpload.HasFile Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("FileFail"))
            Exit Sub

        ElseIf hiFileUpload.PostedFile.ContentType.IndexOf("image") = -1 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("FileNotImage"))
            Exit Sub

        End If


        Dim strFileLongName As String = hiFileUpload.PostedFile.FileName
        Dim strFileName As String = strFileLongName.Substring(strFileLongName.LastIndexOf("\") + 1)
        Dim strFileType As String = strFileName.Substring(strFileName.LastIndexOf(".") + 1)
        Dim strFileShortName As String = strFileName.Substring(0, strFileName.LastIndexOf("."))
        Dim strFileSize As String = hiFileUpload.PostedFile.ContentLength.ToString()


        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("SELECT FilePath ")
        sql.AppendFormat("FROM FileGroup ")
        sql.AppendFormat("WHERE FileGroupID='{0}' ", strClass)

        Dim tmpTable As DataTable = dat.GetData(sql)

        If tmpTable IsNot Nothing AndAlso tmpTable.Rows.Count > 0 Then

            Dim strFileSavePath As String = tmpTable.Rows(0).Item("FilePath")

            Dim strFileSaveLongName As String = (Server.MapPath(strFileSavePath) + strGUID & ".") + strFileType

            Try
                '先刪除檔案
                If System.IO.File.Exists(strFileSaveLongName) Then
                    System.IO.File.Delete(strFileSaveLongName)
                End If

                '將檔案上傳至server 
                hiFileUpload.PostedFile.SaveAs(strFileSaveLongName)
                '將檔案上傳至DataTable 
                ObjectDataSource1.InsertParameters("theClass").DefaultValue = strClass
                ObjectDataSource1.InsertParameters("theType").DefaultValue = strType
                ObjectDataSource1.InsertParameters("FileName").DefaultValue = strFileName
                ObjectDataSource1.InsertParameters("FileShortName").DefaultValue = strFileShortName
                ObjectDataSource1.InsertParameters("FileType").DefaultValue = strFileType
                ObjectDataSource1.InsertParameters("FileSavePath").DefaultValue = strFileSavePath
                ObjectDataSource1.InsertParameters("FileSaveName").DefaultValue = strGUID
                ObjectDataSource1.InsertParameters("FileDesc").DefaultValue = String.Empty
                ObjectDataSource1.InsertParameters("CreateBy").DefaultValue = Page.User.Identity.Name

                ObjectDataSource1.Insert()

                '新增時也要指定
                FormView1.DataSourceID = ObjectDataSource1.ID
                ObjectDataSource1.SelectParameters.Item("FileSaveName").DefaultValue = ViewState("theGUID")

            Catch ex As Exception
                If System.IO.File.Exists(strFileSaveLongName) Then
                    System.IO.File.Delete(strFileSaveLongName)
                End If
                MyObj.ShowMessage(Me.GetLocalResourceObject("UploadFail"))
            End Try

        End If

    End Sub

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
        If Me.FormView1.DataItem IsNot Nothing Then
            '繫結GoogleMap
            TryCast(Me.FormView1.FindControl("GoogleMapExif1"), WebForm_CommonControl_GoogleMapExif).PicFilePath = DataBinder.Eval(Me.FormView1.DataItem, "FilePathName").ToString()
        End If
    End Sub
End Class


Partial Class WebForm_CommonControl_UploadPicControl

#Region "Property"

    Public Property strClass() As String
        Get
            Return DirectCast(ViewState("strClass"), String)
        End Get
        Set(ByVal value As String)

            ViewState("strClass") = value
        End Set
    End Property

    Public Property strType() As String
        Get
            Return DirectCast(ViewState("strType"), String)
        End Get
        Set(ByVal value As String)

            ViewState("strType") = value
        End Set
    End Property

    Public Property strGUID() As String

        Get
            If ViewState("theGUID") Is Nothing OrElse ViewState("theGUID") = String.Empty Then
                ViewState("theGUID") = Guid.NewGuid().ToString()
            End If

            Return DirectCast(ViewState("theGUID"), String)

        End Get
        Set(ByVal value As String)

            ViewState("theGUID") = value
            FormView1.DataSourceID = ObjectDataSource1.ID
            ObjectDataSource1.SelectParameters.Item("FileSaveName").DefaultValue = ViewState("theGUID")
        End Set

    End Property

    Public ReadOnly Property FindDirtyPhotoWidth() As Integer
        Get
            If ViewState("FindDirtyPhotoWidth") Is Nothing Then
                ViewState("FindDirtyPhotoWidth") = MyObj.GetParameter("FindDirtyHeader", "PhotoWidth")
            End If

            Return ViewState("FindDirtyPhotoWidth")
        End Get
    End Property

    Public ReadOnly Property FindDirtyPhotoHeight() As Integer
        Get
            If ViewState("FindDirtyPhotoHeight") Is Nothing Then
                ViewState("FindDirtyPhotoHeight") = MyObj.GetParameter("FindDirtyHeader", "PhotoHeight")
            End If

            Return ViewState("FindDirtyPhotoHeight")
        End Get
    End Property
#End Region

End Class



